package com.hzit.loan.pay.service.impl;

import com.hzit.loan.pay.commonApi.enums.PayWayEnum;
import com.hzit.loan.pay.commonApi.req.PayReq;
import com.hzit.loan.pay.commonApi.resp.PayResultData;
import com.hzit.loan.pay.commonApi.resp.RespVo;
import com.hzit.loan.pay.config.PayConfig;
import com.hzit.loan.pay.factory.PayStrategyFactory;
import com.hzit.loan.pay.model.PaySerial;
import com.hzit.loan.pay.service.IPayStrategyService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

/**
 * 微信扫码支付
 */
@Service
public class WechatQrCodePayImpl implements IPayStrategyService, InitializingBean {



    private Logger logger = LoggerFactory.getLogger(WechatQrCodePayImpl.class);

    @Autowired
    private RestTemplate restTemplate;

    @Autowired
    private PayConfig payConfig;

    @Override
    public RespVo<PayResultData> pay(PaySerial paySerial) {

        //https://api.mch.weixin.qq.com/pay/unifiedorder

//        <xml>
//   <appid>wx2421b1c4370ec43b</appid>
//   <attach>支付测试</attach>
//   <body>JSAPI支付测试</body>
//   <mch_id>10000100</mch_id>
//   <detail><![CDATA[{ "goods_detail":[ { "goods_id":"iphone6s_16G", "wxpay_goods_id":"1001", "goods_name":"iPhone6s 16G", "quantity":1, "price":528800, "goods_category":"123456", "body":"苹果手机" }, { "goods_id":"iphone6s_32G", "wxpay_goods_id":"1002", "goods_name":"iPhone6s 32G", "quantity":1, "price":608800, "goods_category":"123789", "body":"苹果手机" } ] }]]></detail>
//   <nonce_str>1add1a30ac87aa2db72f57a2375d8fec</nonce_str>
//   <notify_url>http://wxpay.wxutil.com/pub_v2/pay/notify.v2.php</notify_url>
//   <openid>oUpF8uMuAJO_M2pxb1Q9zNjWeS6o</openid>
//   <out_trade_no>1415659990</out_trade_no>
//   <spbill_create_ip>14.23.150.211</spbill_create_ip>
//   <total_fee>1</total_fee>
//   <trade_type>JSAPI</trade_type>
//   <sign>0CB01533B8C1EF103065174F50BCA001</sign>
//</xml>

        //封装请求参数格式

        String wechatXml = new StringBuffer()
                .append("<xml>")
                    .append("<appid>").append(payConfig.getWechatAppId()).append("</appid>")
                    .append("<attach>").append("").append("</attach>")

                .append("</xml>")
                .toString();

        //生成签名


         String respXml = restTemplate.postForObject("https://api.mch.weixin.qq.com/pay/unifiedorder",wechatXml,String.class);
        logger.info("respXml = " + respXml);

//        <xml>
//   <return_code><![CDATA[SUCCESS]]></return_code>
//   <return_msg><![CDATA[OK]]></return_msg>
//   <appid><![CDATA[wx2421b1c4370ec43b]]></appid>
//   <mch_id><![CDATA[10000100]]></mch_id>
//   <nonce_str><![CDATA[IITRi8Iabbblz1Jc]]></nonce_str>
//   <openid><![CDATA[oUpF8uMuAJO_M2pxb1Q9zNjWeS6o]]></openid>
//   <sign><![CDATA[7921E432F65EB8ED0CE9755F0E86D72F]]></sign>
//   <result_code><![CDATA[SUCCESS]]></result_code>
//   <prepay_id><![CDATA[wx201411101639507cbf6ffd8b0779950874]]></prepay_id>
//   <trade_type><![CDATA[JSAPI]]></trade_type>
//           <code_url><![CDATA[JSAPI]]></code_url>

//</xml>



        //验签


        //解析 sax解析 xml

        //得到 code_url


        //封装二维码地址


        return null;
    }


    @Override
    public void afterPropertiesSet() throws Exception {
        System.out.println("=======WechatQrCodePayImpl初始化=========");
        PayStrategyFactory.concurrentHashMap.put(PayWayEnum.WECHAT_SM.getCode(),this);

    }

}
